package com.wsdk.util;

import android.app.Activity;
import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.wsdk.R;

/**
 *
 * @author fionera
 * @date 15-11-12
 */
public class ShowDialog {

    public static void showConfirmDialog(Context context, String title, String message,
                                         final View.OnClickListener listener) {
        final AlertDialog alertDialog = new AlertDialog.Builder(context, R.style.ProcessDialog)
                .setCancelable(false).create();
        if (((Activity) context).isFinishing()) {
            return;
        }
        alertDialog.show();
        alertDialog.setContentView(R.layout.dialog_confirm);
        TextView tvTitle = alertDialog.getWindow().findViewById(
                R.id.tv_confirm_dialog_title);
        TextView tvMsg = alertDialog.getWindow().findViewById(
                R.id.tv_confirm_dialog_msg);
        tvTitle.setText(title);
        tvMsg.setText(message);
        alertDialog.getWindow().findViewById(R.id.bt_confirm_dialog_ok).setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        if (listener != null) {
                            listener.onClick(view);
                        }
                        alertDialog.dismiss();
                    }
                });
        alertDialog.getWindow().findViewById(R.id.bt_confirm_dialog_cancel).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        alertDialog.dismiss();
                    }
                });
    }

    public static void showSelectDialog(Context context, String title, String message,
                                        String msgSub, final View.OnClickListener listener) {
        final AlertDialog alertDialog = new AlertDialog.Builder(context, R.style.ProcessDialog)
                .setCancelable(false).create();
        if (((Activity) context).isFinishing()) {
            return;
        }
        alertDialog.show();
        alertDialog.setContentView(R.layout.dialog_alert);
        TextView tvTitle = alertDialog.getWindow().findViewById(
                R.id.tv_edit_dialog_title);
        TextView tvMsg = alertDialog.getWindow().findViewById(R.id.tv_select_dialog_msg);
        TextView tvSubMsg = alertDialog.getWindow().findViewById(
                R.id.tv_select_dialog_submsg);
        tvTitle.setText(title);
        tvMsg.setText(message);
        tvSubMsg.setText(msgSub);
        if (TextUtils.isEmpty(message)) {
            tvMsg.setVisibility(View.GONE);
        }
        if (TextUtils.isEmpty(msgSub)) {
            tvSubMsg.setVisibility(View.GONE);
        }
        alertDialog.getWindow().findViewById(R.id.tv_select_dialog_cancel).setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        alertDialog.dismiss();
                    }
                });
        alertDialog.getWindow().findViewById(R.id.tv_select_dialog_ok).setOnClickListener(
                new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onClick(view);
                alertDialog.dismiss();
            }
        });
    }

    public static void showSelectDialog(Context context, String title, String message,
                                        String msgSub, final View.OnClickListener listener,
                                        final View.OnClickListener dealCancel) {
        final AlertDialog alertDialog = new AlertDialog.Builder(context, R.style.ProcessDialog)
                .setCancelable(false).create();
        if (((Activity) context).isFinishing()) {
            return;
        }
        alertDialog.show();
        alertDialog.setContentView(R.layout.dialog_alert);
        TextView tvTitle = alertDialog.getWindow().findViewById(
                R.id.tv_edit_dialog_title);
        TextView tvMsg = alertDialog.getWindow().findViewById(R.id.tv_select_dialog_msg);
        TextView tvSubMsg = alertDialog.getWindow().findViewById(
                R.id.tv_select_dialog_submsg);
        tvTitle.setText(title);
        tvMsg.setText(message);
        tvSubMsg.setText(msgSub);
        if (TextUtils.isEmpty(message)) {
            tvMsg.setVisibility(View.GONE);
        }
        if (TextUtils.isEmpty(msgSub)) {
            tvSubMsg.setVisibility(View.GONE);
        }
        alertDialog.getWindow().findViewById(R.id.tv_select_dialog_cancel).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                view.setTag(alertDialog);
                dealCancel.onClick(view);
                alertDialog.dismiss();
            }
        });
        alertDialog.getWindow().findViewById(R.id.tv_select_dialog_ok).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                view.setTag(alertDialog);
                listener.onClick(view);
                alertDialog.dismiss();
            }
        });
    }

    public static void showEditDialog(Context context, String title, String message,
                                      View.OnClickListener listener) {
        showEditDialog(context, title, message, listener, "请输入");
    }

    public static void showEditDialog(Context context, String title, String message,
                                      final View.OnClickListener listener, String hint) {
        final AlertDialog alertDialog = new AlertDialog.Builder(context, R.style.ProcessDialog)
                .setCancelable(false).create();
        if (((Activity) context).isFinishing()) {
            return;
        }
        alertDialog.show();
        alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        alertDialog.setContentView(R.layout.dialog_edit);
        TextView tvTitle = alertDialog.getWindow().findViewById(
                R.id.tv_edit_dialog_title);
        TextView tvMsg = alertDialog.getWindow().findViewById(R.id.tv_edit_dialog_msg);
        final EditText etReason = alertDialog.getWindow().findViewById(
                R.id.et_edit_dialog_reason);

        tvTitle.setText(title);
        tvMsg.setText(message);
        if (TextUtils.isEmpty(message)) {
            tvMsg.setVisibility(View.GONE);
        }
        alertDialog.getWindow().findViewById(R.id.tv_edit_dialog_cancel).setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        alertDialog.dismiss();
                    }
                });
        alertDialog.getWindow().findViewById(R.id.tv_edit_dialog_ok).setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        view.setTag(etReason.getText().toString());
                        listener.onClick(view);
                        alertDialog.dismiss();
                    }
                });
    }
}
